package com.qn.wechat.controller;

import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.mybatis.plugin.pagination.PageParams;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.oneplatform.base.exception.ExceptionCode;
import com.oneplatform.base.model.PageResult;
import com.qn.wechat.ifservice.dto.WechatAccountDto;
import com.qn.wechat.ifservice.dto.para.WeChatAccountQueryPara;
import com.qn.wechat.service.WeChatAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.webresources.WarResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
//@Api(value = "/wechataccount",tags = "微信公众号管理")
@RequestMapping("/wechataccount")
public class WeChatAccountController {

    @Resource
    WeChatAccountService weChatAccountService;

    @ApiOperation(value = "获取公众号列表")
    @RequestMapping(value = "/wechat_account_list_get", method = RequestMethod.POST)
    public @ResponseBody
    PageResult<WechatAccountDto> getWeChatAccountList(@ModelAttribute WeChatAccountQueryPara para) {

        Page<WechatAccountDto> page = weChatAccountService.getWeChatAccountList(para);
        return new PageResult<WechatAccountDto>(page.getPageNo(), page.getPageSize(), page.getTotal(), page.getData());
    }

    @ApiOperation(value = "新增、修改公众号（wid=0的时候为新增）")
    @RequestMapping(value = "/wechat_account_save", method = RequestMethod.POST)
    public @ResponseBody
    WrapperResponse<String> saveWeChatAccount(@RequestBody WechatAccountDto dto) {
        return weChatAccountService.saveWeChatAccount(dto);
    }

    @ApiOperation(value = "获取公众号账号")
    @RequestMapping(value = "/wechat_account_get", method = RequestMethod.GET)
    public @ResponseBody
    WrapperResponse<WechatAccountDto> get(@ModelAttribute WeChatAccountQueryPara para) {

        WrapperResponse<WechatAccountDto> response = new WrapperResponse<>();
        para.setPageNo(1);
        para.setPageSize(1);
        AssertUtil.isTrue(para.getWid() != 0, ExceptionCode.REQUEST_PARAM_ERROR.code, "wid不能为空");

        Page<WechatAccountDto> page = weChatAccountService.getWeChatAccountList(para);
        if (page != null && page.getData() != null && page.getData().size() > 0) {
            response.setData(page.getData().get(0));
        }

        return response;

    }
}
